package com.sgu6.sgu6_machine.ui.fragment.set;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.middleware.util.HexUtil;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 警报设置
 */
public class ModeF extends BaseF {

    @BindView(R.id.tv_mode)
    TextView tvMode;
    @BindView(R.id.btn_open_door_mode)
    Button btnOpenDoorMode;
    @BindView(R.id.btn_close_door_mode)
    Button btnCloseDoorMode;

    public static ModeF newInstance() {
        return new ModeF();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mode, container, false);
        unbinder = ButterKnife.bind(this, v);

        tvMode.setText("当前模式：".concat(getModeString()).concat("，").concat(getGarnerString()));
        if (MachineContainer.machineConfigDto.withPack){
            btnOpenDoorMode.setVisibility(View.GONE);
            btnCloseDoorMode.setVisibility(View.GONE);
        }

        return v;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.btn_close_door_mode, R.id.btn_close_lock_mode, R.id.btn_open_send_valley, R.id.btn_close_send_valley, R.id.btn_open_door_mode, R.id.btn_open_lock_mode, R.id.btn_open_lamp, R.id.btn_close_lamp, R.id.btn_A, R.id.btn_B, R.id.btn_AB})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_open_send_valley:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.自动送谷模式.code()));
                tvMode.setText("当前模式：自动送谷，".concat(getGarnerString()));
                MachineContainer.machineConfigDto.microControlMode = 0;
                updateConfig();
                break;
            case R.id.btn_close_send_valley:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.不自动送谷模式.code()));
                tvMode.setText("当前模式：不自动送谷，".concat(getGarnerString()));
                MachineContainer.machineConfigDto.microControlMode = 1;
                updateConfig();
                break;
            case R.id.btn_open_door_mode:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.打开开关门模式.code()));
                tvMode.setText("当前模式：开关门，".concat(getGarnerString()));
                MachineContainer.machineConfigDto.microControlMode = 2;
                updateConfig();
                break;
            case R.id.btn_open_lock_mode:
                if (MachineContainer.machineConfigDto.microControlMode != 2) {
                    ToastUtil.showLongToast(requireContext(), "请先打开开关门模式");
                } else {
                    SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.打开电子锁模式.code()));
                    tvMode.setText("当前模式：电子锁，".concat(getGarnerString()));
                    MachineContainer.machineConfigDto.microControlMode = 3;
                    updateConfig();
                }
                break;
            case R.id.btn_open_lamp:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.打开灯箱.code()));
                break;
            case R.id.btn_close_lamp:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.关闭灯箱.code()));
                break;
            case R.id.btn_A:
                tvMode.setText("当前模式：".concat(getModeString()).concat("，A谷仓模式"));
                MachineContainer.machineConfigDto.mode = 1;
                updateConfig();
                StringBuilder sa = new StringBuilder(MachineCmdEnum.胚芽米.code());
                //5AA509820043000100070000
                sa.replace(15, 16, String.valueOf(MachineContainer.machineConfigDto.mode));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(sa.toString()));
                break;
            case R.id.btn_B:
                tvMode.setText("当前模式：".concat(getModeString()).concat("，B谷仓模式"));
                MachineContainer.machineConfigDto.mode = 2;
                updateConfig();
                StringBuilder sb = new StringBuilder(MachineCmdEnum.胚芽米.code());
                //5AA509820043000100070000
                sb.replace(15, 16, String.valueOf(MachineContainer.machineConfigDto.mode));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(sb.toString()));
                break;
            case R.id.btn_AB:
                tvMode.setText("当前模式：".concat(getModeString()).concat("，AB谷仓模式"));
                MachineContainer.machineConfigDto.mode = 3;
                updateConfig();
                StringBuilder sab = new StringBuilder(MachineCmdEnum.胚芽米.code());
                //5AA509820043000100070000
                sab.replace(15, 16, String.valueOf(MachineContainer.machineConfigDto.mode));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(sab.toString()));
                break;
            case R.id.btn_close_door_mode:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.关闭开关门模式.code()));
                break;
            case R.id.btn_close_lock_mode:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.关闭电子锁模式.code()));
                break;
        }
    }

    private String getGarnerString() {
        if (MachineContainer.machineConfigDto.mode == 1) {
            return "A谷仓模式";
        } else if (MachineContainer.machineConfigDto.mode == 2) {
            return "B谷仓模式";
        } else if (MachineContainer.machineConfigDto.mode == 3) {
            return "AB谷仓模式";
        } else {
            return "A谷仓模式";
        }
    }

    private String getModeString() {
        if (MachineContainer.machineConfigDto.microControlMode == 0) {
            return "自动送谷";
        } else if (MachineContainer.machineConfigDto.microControlMode == 1) {
            return "不自动送谷";
        } else if (MachineContainer.machineConfigDto.microControlMode == 2) {
            return "开关门";
        } else if (MachineContainer.machineConfigDto.microControlMode == 3) {
            return "电子锁";
        } else {
            return "自动送谷";
        }
    }
}
